package com.hui.config.security.session;

import com.hui.config.security.constant.AuthError;
import com.hui.config.security.utils.CrosUtil;
import com.hui.dto.JsonResult;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义session管理
 */
public final class RestSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    /**
     * session过期处理
     */
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        HttpServletResponse response = event.getResponse();
        HttpServletRequest request = event.getRequest();
        CrosUtil.crosOrgin(request, response);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(AuthError.SESSION_EXPIRED.msg);
        jsonResult.setCode(AuthError.SESSION_EXPIRED.code);
        response.getWriter().print(jsonResult.toString());
        response.flushBuffer();
    }
}
